package com.yuelan.apartment.controller;

import com.yuelan.apartment.domain.ApaRoom;
import com.yuelan.apartment.domain.vo.ApaRoomVo;
import com.yuelan.apartment.service.ApaRoomService;
import com.yuelan.common.annotation.Log;
import com.yuelan.common.core.controller.BaseController;
import com.yuelan.common.core.domain.AjaxResult;
import com.yuelan.common.core.page.TableDataInfo;
import com.yuelan.common.enums.BusinessType;
import com.yuelan.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @description: 房间信息
 * @Author: ZhaoYi
 * @date: 24-5-23 9:23
 */
@RestController
@RequestMapping(value = "/room")
public class ApaRoomController extends BaseController {

    @Resource
    private ApaRoomService apaRoomService;


    @PostMapping("/export")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "导出房间信息", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response) {
        List<ApaRoomVo> export = apaRoomService.export();
        ExcelUtil<ApaRoomVo> util = new ExcelUtil<>(ApaRoomVo.class);
        util.exportExcel(response, export, "房间信息数据");
    }


    @PostMapping
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "新增房间", businessType = BusinessType.INSERT)
    public AjaxResult addRoom(@Validated @RequestBody ApaRoom apaRoom) {
        return toAjax(apaRoomService.addRoom(apaRoom));
    }


    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "刪除房间", businessType = BusinessType.DELETE)
    public AjaxResult remove(@NotNull @PathVariable Long[] ids) {
        return toAjax(apaRoomService.deleteRoomIds(ids));
    }


    @PutMapping
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "修改房间", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody @Validated ApaRoom apaRoom) {
        return toAjax(apaRoomService.update(apaRoom));
    }


    @GetMapping
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    public AjaxResult load(@RequestParam("id") Long id) {
        return success(apaRoomService.load(id));
    }


    @GetMapping("/list")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    public TableDataInfo list(ApaRoom apaRoom) {
        startPage();
        return getDataTable(apaRoomService.pageList(apaRoom));
    }

}
